﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nage.Algorithms;
using Nage.Algorithms.Solution;
using Nage.Algorithms.RandomGenerators;
using Utils.Logger;

namespace Nage.Examples.Rastrigin.Solution
{
    public class CoordinatesFactory : ISolutionFactory<Coordinates>
    {
        #region Private fields
        private Random _random;
        private double _min;
        private double _max;
        private int _dimensions;
        #endregion

        #region Constructor
        public CoordinatesFactory()
            : this(-5, 5, 2)
        {
        }

        public CoordinatesFactory(double min, double max, int dimensions)
        {
            CLogger.LogTrace("Creating CoordinatesFactory.");
            this._random = new Random();
            this._min = min;
            this._max = max;
            this._dimensions = dimensions;

        }
        #endregion

        #region CreateEmptySolution
        public Coordinates CreateEmptySolution()
        {
            CLogger.LogTrace("CoordinatesFactory -> CreateEmptySolution");
            double[] vector = new double[_dimensions];
            return new Coordinates(vector);
        } 
        #endregion

        #region CreateInitializedSolution
        public Coordinates CreateInitializedSolution()
        {
            CLogger.LogTrace("CoordinatesFactory -> CreateInitializedSolution");
            double[] vector = new double[_dimensions];
            for (int i = 0; i < _dimensions; i++)
            {
                vector[i] = Rng.GetInstance().GetDouble(_min, _max);
            }
            return new Coordinates(vector);
        } 
        #endregion

        #region Copy
        public Coordinates Copy(Coordinates source)
        {
            CLogger.LogTrace("CoordinatesFactory -> Copy");
            return new Coordinates(source.Vector.ToArray());
        } 
        #endregion

        #region ToString
        public override string ToString()
        {
            return String.Format("CoordinatesFactory(min:{0}, max:{1}, dim:{2})", _min, _max, _dimensions);
        } 
        #endregion
    }
}
